<?php

namespace wap\Controller;

use Think\Controller;

class ComplainController extends BaseController {
    public function __construct()
    {
        parent::__construct();
    }

    /*
     * 黑名单设置
     * @date 2017-11-30
     */
    public function blackList()
    {
        //判断医生或者领导是否有添加黑名单的权限
        $user_id = 1;
        $permission = M('BlacklistPermission')->where(['user_id'=>$user_id,'is_pass'=>1])->find();
        if($permission){
            $have_permission = 1;
            $blackData = M('PatientInfo')->where(['is_blacklist'=>1])->select();
        }else{
            $have_permission = 0;
        }
        $permission_check = M('BlacklistPermission')->where(['user_id'=>$user_id,'is_pass'=>0])->find();
        $this->assign('have_permission',$have_permission);
        $this->assign('permission_check',$permission_check);
        $this->assign('blackData',$blackData);
        $this->display('complain/blacklist');
    }

    /*
     * 申请黑名单权限设置
     * @date 2017-11-30
     */
    public function blacklist_permission_set(){
        $user_id = 1;
        $userData = M('UserInfo')->where(['user_id'=>$user_id])->find();
        $depart = M('Department')->where(['id'=>$userData['depart_id']])->getField('de_name');
        $rs = M('BlacklistPermission')->add([
            'user_id' => $user_id,
            'uname' => $userData['user_name'],
            'department' => $depart,
            'add_at'=>time(),
            'update_at'=>time(),
        ]);
        if($rs){
            $this->ajaxReturn(['code'=>1,'msg'=>'审核中']);
        }else{
            $this->ajaxReturn(['code'=>0,'msg'=>'申请失败']);
        }
    }

    /*
     * 要添加的黑名单患者搜索接口
     * @date 2017-11-30
     * @param 搜索内容 string
     */
    public function search_user(){
        $str = I('post.str');
        $userData = M('PatientInfo')->where(['patient_name'=>['like','%'.$str.'%'],'is_blacklist'=>0])->field('patient_id,patient_name,head_img')->select();
        if($userData){
            dataFormat($userData, 0, 'success');
        }else{
            $this->ajaxReturn(['code'=>1,'msg'=>'error']);
        }
    }

    /*
     * 加入黑名单接口
     * @date 2017-11-30
     * @param 患者id字符串
     */
    public function add_blacklist(){
        $ids = trim(I('post.ids'));
        $res = M('PatientInfo')->where(['patient_id'=>['in',$ids]])->setField(['is_blacklist'=>1]);
        if($res){
            $this->ajaxReturn(['code'=>1,'msg'=>'success']);
        }else{
            $this->ajaxReturn(['code'=>0,'msg'=>'error']);
        }
    }

    /*
     * 取消患者的黑名单设置
     * @date 2017-11-30
     * @param 患者id
     */
    public function del_blacklist()
    {
        if(I('post.patient_id')){
            $res = M('PatientInfo')->where(['patient_id'=>I('post.patient_id')])->setField(['is_blacklist'=>0]);
            if($res){
                $this->ajaxReturn(['code'=>1,'msg'=>'success']);
            }else{
                $this->ajaxReturn(['code'=>0,'msg'=>'error']);
            }
        }else{
            $this->ajaxReturn(['code'=>2,'msg'=>'参数有误']);
        }

    }

    /*
     * 领导个人中心页
     * @date 2017-11-30
    */
    public function centerLeader()
    {
        $this->display('complain/center_leader');
    }

    /*
     * 用户个人中心页
     * @date 2017-11-30
    */
    public function centerUser()
    {
        $this->display('complain/center_user');
    }

    /*
     * 院内投诉页
     * @date 2017-11-30
    */
    public function hospital()
    {
        $this->display('complain/hospital');
    }

    /*
     * 患者投诉页
     * @date 2017-11-30
    */
    public function patient()
    {
        $this->display('complain/patient');
    }

    /*
     * 预警信息添加接口
     * @date 2017-11-30
    */
    public function waring_handle(){
        //接收预警信息
        if (I('post.postData')){
            parse_str(htmlspecialchars_decode(I('post.postData')),$postData);
            $waring_data = [
                'name' => $postData['name'],
                'age' => $postData['age'],
                'sex' => $postData['sex'],
                'hospital_num' => $postData['hospital_num'],
                'outpatient_num' => $postData['outpatient_num'],
                'insurance_type' => $postData['insurance_type'],
                'source' => $postData['source'],
                'idCard' => $postData['idCard'],
                'medical_insurance' => $postData['medical_insurance'],
                'phone' => $postData['phone'],
                'relation_name' => $postData['relation_name'],
                'relation_phone' => $postData['relation_phone'],
                'illness_severity' => $postData['illness_severity'],
                'relation_attitude' => $postData['relation_attitude'],
                'medical_type' => $postData['medical_type'],
                'medical_workers' => $postData['medical_workers'],
                'illness_level' => $postData['illness_level'],
                'dispute_possibility' => $postData['dispute_possibility'],
                'medical_complaint' => $postData['medical_complaint'],
                'care_complaint' => $postData['care_complaint'],
                'speak_behavir' => $postData['speak_behavir'],
                'action_level' => $postData['action_level'],
                'hospital_complaint' => $postData['hospital_complain'],
                'pathography' => $postData['pathography'],
                'process_user_id' => $postData['process_user_id'],
                'process_group_user' => $postData['process_group_user'],
            ];
            $rs = M('AlarmMessage')->add($waring_data);
            if($rs){
                $this->ajaxReturn(['code'=>1,'msg'=>'提交成功']);

            }
        }
        $this->display('complain/waring_handle');
    }


    public function subPatientCom()
    {
        $post = I('post.postData');
        parse_str(htmlspecialchars_decode($post), $myArr);
        $visit_str       = $this->timeToStr($myArr['visit_at']);
        $inhospital_str  = $this->timeToStr($myArr['inhospital_at']);
        $outhospital_str = $this->timeToStr($myArr['outhospital_at']);
        $img             = rtrim($myArr['complain_img'], ',');
        $url_str         = [];
        $explode         = explode(',', $img);
        foreach ($explode as $k => $v) {
            $res       = uploadqiniu($v, 'patient');
            $url_str[] = $res;
        }
        $data = [
            'hospital_id'               => 1,
            'type'                      => 0,
            'complain_name'             => $myArr['complain_name'],//投诉人姓名
            'complain_sex'              => $myArr['complain_sex'],//投诉人性别 0-女 1-男
            'complain_age'              => $myArr['complain_age'],//投诉人年龄
            'complain_mobile'           => $myArr['complain_mobile'],//投诉人手机号
            'complain_wechat'           => $myArr['complain_wechat'],//投诉人微信
            'visit_at'                  => strtotime($visit_str),//就诊时间
            'outpatient_num'            => $myArr['outpatient_num'],//门诊号
            'inhospital_at'             => strtotime($inhospital_str),//住院时间
            'outhospital_at'            => strtotime($outhospital_str),//出院时间
            'complained_name'           => $myArr['complained_name'],//被投诉人姓名
            'complained_depart'         => $myArr['complained_depart'],//被投诉科室
            'complain_type'             => $myArr['complain_type'],//投诉类型
            'complain_content'          => $myArr['complain_content'],//投诉内容
            'complain_img'              => implode(',',$url_str),//图片
            'is_self'                   => $myArr['is_self'],//是否患者本人 0-否，1-是',
            'family_name'               => $myArr['family_name'],//家属姓名
            'family_sex'                => $myArr['family_sex'],//家属性别 0-女 1-男
            'family_age'                => $myArr['family_age'],//家属年龄
            'family_mobile'             => $myArr['family_mobile'],//家属手机号
            'family_wechat'             => $myArr['family_wechat'],//家属微信
            'relationship_with_patient' => $myArr['relationship_with_patient'],//与患者关系
            'add_at'                    => time(),
            'update_at'                 => time()
        ];
        $add  = M('ComplainInfo')->add($data);
        if($add){
            exit(json_encode(['code'=>0,'msg'=>'success']));
        }else{
            exit(json_encode(['code'=>1,'msg'=>'fail']));
        }

    }

    public function subinHospitalCom()
    {
        $post = I('post.postData');
        parse_str(htmlspecialchars_decode($post), $myArr);
        $img     = rtrim($myArr['hos_by_people_imgurl'], ',');
        $url_str = [];
        $explode = explode(',', $img);
        foreach ($explode as $k => $v) {
            $res       = uploadqiniu($v, 'hos');
            $url_str[] = $res;
        }
        $data = [
                'hospital_id'               => 1,
                'type'                      => 1,
                'complain_name'             => $myArr['hos_people_name'],
                'complain_sex'              => $myArr['hos_people_sex'],
                'complain_age'              => $myArr['hos_people_age'],
                'complain_mobile'           => $myArr['hos_people_phone'],
                'complain_wechat'           => $myArr['hos_people_wx'],
                'complained_name'           => $myArr['hos_by_people_name'],
                'complained_depart'         => $myArr['hos_by_people_admin'],
                'complain_type'             => $myArr['hos_by_people_status'],
                'complain_content'          => $myArr['hos_by_people_content'],
                'complain_img'              => implode(',',$url_str),
                'add_at'                    => time(),
                'update_at'                 => time()
            ];
        $add = M('ComplainInfo')->add($data);
        if($add){
            exit(json_encode(['code'=>0,'msg'=>'success']));
        }else{
            exit(json_encode(['code'=>1,'msg'=>'fail']));
        }
    }

    public function timeToStr($str)
    {
        $yearArr  = explode('年', $str);
        $year     = $yearArr[0];
        $monthArr = explode('月', $yearArr[1]);
        $month    = $monthArr[0] < 10 ? '0' . $monthArr[0] : $monthArr[0];
        $dayArr   = explode('日', $monthArr[1]);
        $day      = $dayArr[0] < 10 ? '0' . $dayArr[0] : $dayArr[0];
        $time     = $year . '-' . $month . '-' . $day;

        return $time;
    }

    public function upFile()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     0 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     $_SERVER['DOCUMENT_ROOT'] . '/../../statics/upload/image/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            exit(json_encode(['code'=>1,'msg'=>'上传失败']));
        }else{
            exit(json_encode(['code'=>0,'msg'=>UPLOAD_PATH.'/upload/image/'.$info['file']['savepath'].$info['file']['savename']]));
        }
    }
}
